﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.ObjectModel;

namespace Web4Comment
{
    public partial class profile : System.Web.UI.Page
    {
        private string username;
        private UnlessComment.Model.UserTotal userTotal;
        private string hostname;
        private bool isFriend;



        protected void Page_Load(object sender, EventArgs e)
        {
            hostname = (String)Session["username"];
            if (hostname == null || hostname == "")
            {
                Response.Redirect("./logon.aspx");
            }

            username = Request.QueryString["user"];
            if (username == null || username == "" || Service.UserService.existUsername(username) == 0)
            {
                Response.Redirect("./profile.aspx?user=" + hostname);
            }

            if (!IsPostBack)
            {
                userTotal = Service.UserService.getUserTotal(username);
                usernameDataLabel.Text = userTotal.user.username;
                sexDataLabel.Text = userTotal.userInfo.sex == "1" ? "男" : "女";
                locationDataLabel.Text = userTotal.userInfo.location;
                hobbyDataLabel.Text = userTotal.userInfo.screen_name;

                if (username == hostname)
                {
                    friendButton.Text = "编辑";
                    FriendType.Visible = false;
                    modifyButton.Visible = false;
                    long x = Web4Comment.Service.ChatTimeService.logMaintainStartTime((Int32)Session["user_id"], DateTime.Now, 0);
                    logID.Text = x.ToString();
                }
                else
                {
                    
                    isFriend = Service.FriendsService.checkIsFriend(hostname, username);
                    if (isFriend)
                    {
                        friendButton.Text = "删除好友";
                        modifyButton.Visible = true;
                        FriendType.Visible = true;
                        UnlessComment.Model.Friend friendType = Service.FriendsService.getFriendType(hostname, username);
                        if (friendType != null)
                        {
                            schoolCheckBox.Checked = friendType.by_school;
                            companyCheckBox.Checked = friendType.by_company;
                            addressCheckBox.Checked = friendType.by_address;
                            hobbyCheckBox.Checked = friendType.by_hobby;
                        }
                    }
                    else
                    {
                        friendButton.Text = "添加好友";
                        modifyButton.Visible = false;
                        FriendType.Visible = false;
                    }

                    long x = Web4Comment.Service.ChatTimeService.logFriendStartTime((Int32)Session["user_id"], DateTime.Now, userTotal.user.id);
                    logID.Text = x.ToString();

                }

                List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyComments(username);
                Collection<UnlessComment.Model.CommentDetail> comments = new Collection<UnlessComment.Model.CommentDetail>();

                if (commentsList != null)
                {
                    nowLabel.Text = "" + 1;
                    allLabel.Text = "" + (commentsList.Count / 5 + (commentsList.Count % 5 == 0 ? 0 : 1));
                    listBind(commentsList.GetRange(0, commentsList.Count < 5 ? commentsList.Count : 5));
                }
                else
                {
                    nowLabel.Text = "1";
                    allLabel.Text = "0";
                }

                
            }



        }



        protected void friendButton_Click(object sender, EventArgs e)
        {
            if (username == hostname)
            {
                Response.Redirect("./edit.aspx");
            }
            else if (Service.FriendsService.checkIsFriend(hostname, username))
            {
                Service.FriendsService.deleteFriends(hostname, username);
                friendButton.Text = "添加好友";
                modifyButton.Visible = false;
                FriendType.Visible = false;
            }
            else
            {
                Service.FriendsService.addFriends(hostname, username);
                friendButton.Text = "删除好友";
                modifyButton.Visible = true;
                FriendType.Visible = true;
            }
        }

        protected void preButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            if (now <= 1)
            {
                return;
            }
            now--;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyComments(username);
            listBind(commentsList.GetRange((now - 1) * 5, commentsList.Count < now * 5 ? commentsList.Count - (now - 1) * 5 : 5));
        }

        protected void nextButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            if (now >= all)
            {
                return;
            }
            now++;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyComments(username);
            listBind(commentsList.GetRange((now - 1) * 5, commentsList.Count < now * 5 ? commentsList.Count - (now - 1) * 5 : 5));
        }

        protected void goButton_Click(object sender, EventArgs e)
        {
            int now = System.Int32.Parse(nowLabel.Text);
            int all = System.Int32.Parse(allLabel.Text);
            int go = System.Int32.Parse(goTextBox.Text);
            if (go > all || go < 1)
            {
                return;
            }
            now = go;
            nowLabel.Text = now.ToString();
            List<UnlessComment.Model.CommentDetail> commentsList = Web4Comment.Service.CommentService.getMyComments(username);
            listBind(commentsList.GetRange((now - 1) * 5, commentsList.Count < now * 5 ? commentsList.Count - (now - 1) * 5 : 5));
        }

        private void listBind(List<UnlessComment.Model.CommentDetail> commentsList)
        {
            Collection<UnlessComment.Model.CommentDetail> comments = new Collection<UnlessComment.Model.CommentDetail>();

            if (commentsList == null || commentsList.Count == 0)
            {
                commentsdl.DataSource = null;
                commentsdl.DataBind();
                return;
            }

            for (int i = 0; i < commentsList.Count; i++)
            {
                comments.Add(commentsList[i]);
            }

            commentsdl.DataSource = comments;
            commentsdl.DataBind();
        }

        protected void startButton_Click(object sender, EventArgs e)
        {

        }

        protected void endButton_Click(object sender, EventArgs e)
        {
            Web4Comment.Service.ChatTimeService.logEndTime(System.Int64.Parse(logID.Text), DateTime.Now);
        }

        protected void modifyButton_Click(object sender, EventArgs e)
        {
            Web4Comment.Service.FriendsService.modifyFriend(hostname, username, schoolCheckBox.Checked, companyCheckBox.Checked, addressCheckBox.Checked, hobbyCheckBox.Checked);
        }

        public void endButton_Click()
        {
            Web4Comment.Service.ChatTimeService.logEndTime(System.Int64.Parse(logID.Text), DateTime.Now);
        }
    }
}